<template>
  <el-dialog
    v-model="is_show"
    :close-on-click-modal="false"
    append-to-body
    :title="workFrom.title"
    width="600"
    class="dialogForm"
    :before-close="beforeClose"
  >
    <el-form
      :model="addFrom"
      :rules="rulesForm"
      label-width="110px"
      ref="ruleFormRef"
    >
      <el-form-item label="退回到人员" prop="belongId">
        <el-input v-model="addFrom.belongName" disabled></el-input>
      </el-form-item>
      <el-form-item label="退回原因" prop="remark">
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="addFrom.remark"
          maxlength="100"
          show-word-limit
        >
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="beforeClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="btnLoading">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup name="taskBack">
import { getTaskDeal } from '@/api/taskManage/task'
import { reactive, getCurrentInstance, toRefs } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import useCommonStore from '@/store/modules/common'
import { addManage } from '@/api/system/notice'
const { proxy } = getCurrentInstance()
// 通用数据store
const router = useRouter()
const route = useRoute()
const commonStore = useCommonStore()
const userStore = useUserStore()
const is_show = computed(() => {
  return true
})
const data = reactive({
  addFrom: {
    belongId: '',
    remark: ''
  },
  gettime: '',
  // 表单校验
  rulesForm: {
    belongId: [{ required: true, message: '退回到人员', trigger: 'blur' }],
    remark: [{ required: true, message: '退回原因', trigger: 'blur' }]
  }
})
const { addFrom, rulesForm, gettime } = toRefs(data)
const props = defineProps({
  workFrom: {
    type: Object
  }
})
const btnLoading = ref(false) //按钮加载状态

//关闭
function beforeClose() {
  proxy.$emit('closeForm')
}
//保存
function handleSubmit() {
  proxy.$refs['ruleFormRef'].validate(valid => {
    if (valid) {
      let params = {}
      params.remark = addFrom.value.remark
      params.id = props.workFrom.operateId //操作id
      params.targetId = props.workFrom.createBy
      params.targetName = props.workFrom.createName
      params.taskId = props.workFrom.taskId
      params.dealStatus = 3
      btnLoading.value = true
      let array = []
      getTaskDeal(params)
        .then(res => {
          if (res.code === 200) {
            proxy.$modal.msgSuccess('操作成功')
            btnLoading.value = false
            array = res.data
            console.log('array', array, array !== undefined && array.length > 0)
            for (let i = 0; i < array.length; i++) {
              let mesVoData = {
                businessId: '退回任务-通知-' + array[i].operateId, //业务ID
                content: `${props.workFrom.titleName}:${params.remark}`,
                followDate: undefined, //跟进日期
                followType: undefined, //跟进方式
                impSign: props.workFrom.impSign, //重要程度:0-非常重要，1-重要，2-一般
                receiver: array[i].belongId, //接收人id
                receiverName: array[i].belongName, //接收人中文
                reminderMethod: '0', //提醒方式:0-系统站内，1-邮箱，2-短信，允许多个并存，例如:0,1,2
                sendTime: gettime.value, //发送时间
                sender: userStore.user.nickName, //发送人
                title: `您有一条'${props.workFrom.titleName}'被退回的任务`, //待办标题
                type: 0, //待办类型:0-普通，1-流程
                serviceType: '10', //服务类型  0-项目，1-订单，2-客户，3-工单，4-任务，5-产品，6-合同，7-公告，8-日程，9-回款
                urgFlag: '2', //紧急程度:0-非常紧急，1-紧急，2-一般
                urlName: '前往处理', //待办链接名称(多链接名称用英文逗号隔开)
                url: `/rsgl/other/taskProcessing?id=${array[i].taskId}&operateId=${array[i].operateId}` //待办链接(多链接用英文逗号隔开)
              }
              console.log('mesVoData==>', mesVoData)
              addManage(mesVoData)
            }
            router.push({
              path: '/rsgl/other/taskManage'
            })
            beforeClose()
          }
        })
        .catch(res => {
          btnLoading.value = false
        })
    }
  })
}
//获取当前时间
function getTime() {
  var _this = this
  let yy = new Date().getFullYear()
  var mm =
    new Date().getMonth() > 9
      ? new Date().getMonth() + 1
      : new Date().getMonth() == 9
      ? new Date().getMonth() + 1
      : '0' + (new Date().getMonth() + 1)
  var dd =
    new Date().getDate() < 10
      ? '0' + new Date().getDate()
      : new Date().getDate()
  let hh = new Date().getHours()
  let mf =
    new Date().getMinutes() < 10
      ? '0' + new Date().getMinutes()
      : new Date().getMinutes()
  let ss =
    new Date().getSeconds() < 10
      ? '0' + new Date().getSeconds()
      : new Date().getSeconds()
  gettime.value = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
  console.log('gettime', gettime.value)
}
function init() {
  console.log('props', props.workFrom)
  addFrom.value.belongId = props.workFrom.createBy
  addFrom.value.belongName = props.workFrom.createName
  getTime()
}
init()
</script>
